<template>
	<view class="box">
		<view class="hearder">
			<view class="left" @click="goBack">
				<image src="https://s1.ax1x.com/2022/11/26/ztJzTO.png" mode="widthFix"></image>
				返回
			</view>
			<view class="center">我的车辆</view>
			<view class="right" @click="add">添加车辆</view>
		</view>
		<view class="banner">
			<image src="https://s1.ax1x.com/2022/12/02/zBCHb9.png" mode="widthFix"></image>
		</view>
		<view class="cartlist">
			<checkbox-group>
				<label class="row" v-for="(item,index) in cartList" :key="index" >
					<view class="carno">
						<image src="https://s1.ax1x.com/2022/12/02/zBCv8K.png"></image>
						<view class="">{{item.carno}}</view>
					</view>
					<view>
						<checkbox @click="IDs(item.id)" class="check" :value="item.value" :checked="item.checked" />
					</view>
				</label>
			</checkbox-group>
		</view>
		<button class="btn" @click="delCart">删除</button>
	</view>
</template>

<script>
	import {
		carlist,delscar
	} from '../../utils/api.js'
	export default {
		data() {
			return {
				checked: false,
				cartList: [],
				ids:[]
			}
		},
		onLoad() {
			this.getCartList()
		},
		methods: {
			// 返回
			goBack() {
				uni.switchTab({
					url: '../mine'
				})
			},
			async getCartList() {
				let res = await carlist()
				console.log(res);
				if (res.errCode === 10000) {
					this.cartList = res.data.list
				}
			},
			IDs(id) {
				/* 用到两个数组方法: includes和splice;
				 includes查找当前元素有没有在数组中 -> true / false 不会改变原数组
				 如果已经存在了把当前id删除splice(index, 1) // 会改变原始数组
				 如果还没有push 当前id
				 */
				console.log(id);
				if(!this.ids.includes(id)){
					this.ids.push(id)
				}else {
					// 当前id已经存在 -> 删除
					const index = this.ids.findIndex(curid => curid === id)
					this.ids.splice(index, 1)
				}
				console.log('即将要删除的车辆id',this.ids)
			},
			async delCart(){
				let res = await delscar({ids:this.ids})
				console.log(res);
				if(res.errCode==10000){
					uni.showToast({
						title:'删除成功',
						icon:'success'
					})
					this.getCartList()
				}else{
					uni.showToast({
						title:'删除失败',
						icon:'error'
					})
				}
			},
			add() {
				uni.navigateTo({
					url: './addcar/addcar'
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.box {
		width: 100%;
		height: 100%;
		font-size: 24rpx;

		.hearder {
			width: 100%;
			height: 135rpx;
			background-color: #fff;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 20rpx;
			box-sizing: border-box;

			.center {
				font-size: 30rpx;
			}

			.left image {
				width: 15rpx;
				vertical-align: bottom;
				margin-right: 10rpx;
			}

			.right {
				color: #0387ff;
			}
		}

		.banner image {
			width: 100%;
		}

		.carlist {
			width: 100%;
			padding-left: 40rpx;
			box-sizing: border-box;
		}

		.row {
			width: 100%;
			height: 112rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-left: ;
			border-bottom: 1px solid #e9e9e9;
		}

		.carno {
			display: flex;
			align-items: center;

			image {
				margin-left: 50rpx;
				width: 40rpx !important;
				height: 30rpx !important;
				margin-right: 35rpx;
			}
		}

		.check {
			margin-right: 50rpx;
		}

		.btn {
			width: 635rpx;
			height: 95rpx;
			margin: 0 auto;
			border-radius: 45rpx;
			border: 0;
			background-color: #1f8fff;
			color: #fff;
			margin-top: 120rpx;
		}
	}
</style>
